<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <ButtonGroup>
          <a-button type="primary" @click="handleCreate"> 新增 </a-button>
          <!-- <BasicImport @success="handleImport" :api="uploadTenantApi" :tempApi="tempTenantApi">
            <a-button type="info"> 导入 </a-button>
          </BasicImport>
          <a-button type="info" @click="handleExport"> 导出 </a-button> -->
        </ButtonGroup>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                label: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                label: '删除',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <TenantModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { Button } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  // import { BasicImport } from '/@/components/ImportButton';
  import TenantModal from './TenantModal.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';

  import { getTenantListByPage, deleteTenantApi } from '/@/api/admin/tenant';
  import { columns, searchFormSchema } from './tenant.data';
  const ButtonGroup = Button.Group;

  export default defineComponent({
    name: 'TenantManagement',
    components: { ButtonGroup, BasicTable, TenantModal, TableAction },
    setup() {
      const [registerModal, { openModal }] = useModal();

      const { createMessage } = useMessage();

      const [registerTable, { reload }] = useTable({
        api: getTenantListByPage,
        columns,
        // descs
        beforeFetch(param) {
          return {
            ...param,
            descs: 'create_time',
          };
        },
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        // bordered: true,
        // showIndexColumn: false,
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        console.log(record);
        await deleteTenantApi(record.id);
        createMessage.success('操作成功');
        reload();
      }

      function handleSuccess() {
        reload();
      }
      function handleImport() {
        createMessage.success('操作成功');
        reload();
      }

      return {
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleImport,
      };
    },
  });
</script>
